package com.dodoke.ojserver.typeHandlers;

import com.dodoke.ojserver.common.Difficulty;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.sql.*;

@MappedTypes({Difficulty.class})
@MappedJdbcTypes({JdbcType.VARCHAR})
public class DifficultyTypeHandler extends BaseTypeHandler<Difficulty> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Difficulty parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, parameter.name());
    }

    @Override
    public Difficulty getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String name = rs.getString(columnName);
        return Difficulty.valueOf(name);
    }

    @Override
    public Difficulty getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String name = rs.getString(columnIndex);
        return Difficulty.valueOf(name);
    }

    @Override
    public Difficulty getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String name = cs.getString(columnIndex);
        return Difficulty.valueOf(name);
    }
}
